package io;

import java.io.File;
import java.util.Scanner;

public class Demo6 {
    public static void scanDir(File rootPath, String word) {
        File[] files = rootPath.listFiles();
        if (files == null) {
            return;
        }
        for (File f : files) {
            //日志
            System.out.println("当前扫描的文件" + f.getAbsolutePath());
            if (f.isFile()) {
                //检查文件是否要删除
                checkDelete(f, word);
            } else {
                //递归
                scanDir(f, word);
            }
        }
    }

    private static void checkDelete(File f, String word) {
        if (!f.getName().contains(word)) {
            return;
        }
        System.out.println("当前文件为:" + f.getAbsolutePath() + ",请确认是否要删除(Y/N):");
        Scanner scanner = new Scanner(System.in);
        String choice = scanner.next();
        if (choice.equals("Y") || choice.equals("y")) {
            f.delete();
            System.out.println("删除完毕!");
        } else {
            System.out.println("取消删除!");
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的路径:");
        String path = scanner.next();
        File rootPath = new File(path);
        if (!rootPath.isDirectory()) {
            System.out.println("您输入的扫描路径有误!!!");
            return;
        }

        System.out.println("请输入要删除文件的关键词:");
        String word = scanner.next();

        scanDir(rootPath, word);
    }
}
